package com.pro01.backend.controller;

import com.pro01.backend.pojo.PageResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import com.pro01.backend.service.AdminConnectionService;
import com.pro01.backend.dto.UserConnectionStatusDTO;
import com.pro01.backend.pojo.Result;
import com.pro01.backend.utils.TokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/connection")
public class AdminConnectionController {

    @Autowired
    private AdminConnectionService adminConnectionService;

    @GetMapping("/users")
    public Result<PageResult<UserConnectionStatusDTO>> getAllUserConnections(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            HttpServletRequest request) {

        if (!"ADMIN".equals(TokenUtil.getRole(request))) {
            return Result.error("无权限，仅管理员可用");
        }

        PageResult<UserConnectionStatusDTO> result =
                adminConnectionService.getAllUserConnections(pageNum, pageSize);

        return Result.success(result);
    }
}

